﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Elight.WebApi.Filters;
using Microsoft.Extensions.Caching.Memory;

namespace Elight.WebApi.Controllers
{
    //[Authorize, AccessChecked]
    [Route("api/[controller]")]
    public class AccountController : BaseController
    {
        private IMemoryCache _cache;

        public AccountController(IMemoryCache memoryCache)
        {
            _cache = memoryCache;
        }

        // GET: api/values
        [HttpGet]
        public IActionResult Get()
        {
            var result = _cache.GetOrCreate("myKey", (entry) =>
            {
                entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60);
                return DateTime.Now.ToString();
            });

            return Content(result);
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public int Get(int id)
        {
            if (!_cache.TryGetValue("key", out int value))
            {
                _cache.Set("key", id);
                return id;
            }
            else
            {
                return value;
            }
            
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
